home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / chap02 / howto02 / delphi10 / drwsutl2.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-10-14  |  52.6 KB  |  1,426 lines

  1. unit Drwsutl2;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, ShellAPI, FileCtrl, DRWSUtl1;
  8.  
  9. const
  10.   EOC_CHANGEDIR = 1;  { Error Operation Code for change directory failure }
  11.   EOC_SOURCECOPY = 2; { Error Operation Code for source copy failure      }
  12.   EOC_DESTCOPY = 3;   { Error Operation Code for destination copy failure }
  13.   EOC_DELETEFILE = 4; { Error Operation Code for file delete failure      }
  14.   EOC_DELETEDIR = 5;  { Error Operation Code for directory delete failure }
  15.   EOC_RENAMEFILE = 6; { Error Operation Code for renaming failure         }
  16.   EOC_MAKEDIR = 7;    { Error Operation Code for MkDir failure            }
  17.   EOC_SETATTR = 8;    { Error Operation Code for Set Attributes failure   }
  18.  
  19.   FAC_COPY = 1;       { File Action Code for recursive copying            }
  20.   FAC_MOVE = 2;       { File Action Code for recursive moving             }
  21.   FAC_DELETE = 3;     { File Action Code for recursive deletion           }
  22. type
  23.   TFileWorkBench = class( TComponent )
  24.   public
  25.     GlobalError        : Integer;  { This is used by FMXUCopyFile for er code }
  26.     GlobalErrorType    : Integer;  { This holds the Operation code            }
  27.     function ForceTrailingBackSlash( const TheFileName : String ) : String;
  28.     function StripNonRootTrailingBackSlash(
  29.               const TheFileName : String ) : String;
  30.     procedure GetFileAttributes( TheFile : String; var IsDirectory , IsArchive ,
  31.                 IsVolumeID , IsHidden , IsReadOnly , IsSysFile : Boolean );
  32.     procedure HandleIOException( TheOpCode : Integer; ThePath : String;
  33.                                  TheMessage : String; TheCode : Integer );
  34.     procedure HandleDOSError( TheOpCode : Integer; ThePath : String;
  35.                 TheCode : Integer );
  36.     procedure FMXUCopyFile(const FileName, DestName: String);
  37.     function CopyFile( TargetPath ,
  38.                DestinationPath : String ) : Boolean;
  39.     procedure ChangeTheDirectory( NewPath : String );
  40.     procedure ChangeTheDriveAndDirectory( NewDrive : Integer );
  41.     procedure CopyTheFile( OldPath , NewPath : String );
  42.     procedure MoveTheFile( OldPath , NewPath : String );
  43.     procedure DeleteTheFile( ThePath : String );
  44.     procedure RenameTheFile( OldPath , NewName : String );
  45.     procedure CreateNewDirectory( NewPath : String );
  46.     procedure RemoveDirectory( ThePath : String );
  47.   end;
  48.   TFileIconPanel = class( TPanel )
  49.   private
  50.     { Private declarations }
  51.     FHighlightColor : TColor;                 { This holds bright edge bevel }
  52.     FShadowColor    : TColor;                 { This holds dark edge bevel   }
  53.     procedure TheClick( Sender : TObject );   { This holds override click    }
  54.   protected                                   { event method procedure.      }
  55.     { Protected declarations }
  56.     procedure Paint; override;                { This allows custom painting  }
  57.   public
  58.     { Public declarations }
  59.     FTheIcon : TIcon;                         { This is the display icon    }
  60.     FTheName : String;                        { This is the filename        }
  61.     FTheLabel : TLabel;                       { This is the display label   }
  62.     Selected : Boolean;                       { This holds selection status }
  63.     constructor Create(AOwner : TComponent); override; { override create    }
  64.     procedure Initialize( PanelX              ,             { Left          }
  65.                           PanelY              ,             { Top           }
  66.                           PanelWidth          ,             { Width         }
  67.                           PanelHeight         ,             { Height        }
  68.                           PanelBevelWidth     ,             { Bevel Width   }
  69.                           LabelFontSize         : Integer;  { Font size     }
  70.                           PanelColor          ,             { Main color    }
  71.                           PanelHighlightColor ,             { Bright color  }
  72.                           PanelShadowColor    ,             { Dark color    }
  73.                           LabelTextColor        : TColor;   { Text color    }
  74.                           TheFilename         ,             { Filename      }
  75.                           LabelFontName         : String;   { Font name     }
  76.                           LabelFontStyle        : TFontStyles;  { Font style}
  77.                           ExtraData             : Integer       );  { Drive }
  78.     destructor Destroy; override;             { override destroy to free    }
  79.   end;
  80.   TFileIconPanelScrollBox = class( TScrollBox )
  81.   public
  82.     { Public methods and data }
  83.     TheFWB              : TFileWorkBench; { Used for file manipulation         }
  84.     IconsNeedRefreshing : Boolean;                   { Flag to redo display    }
  85.     TheIconSize        : Integer;   { Holds Individual Icon size               }
  86.     TheIconSpacing     : Integer;   { Holds total icon footprint               }
  87.     MaxIconsInARow     : Integer;   { Set for screen size.                     }
  88.     TheStoredHandle    : HWnd;
  89.     procedure Update;                                { Called to reset display }
  90.     constructor Create( AOwner : TComponent ); override;  { Override inherited }
  91.     procedure ClearTheFIPs;                          { Clears the FIPs safely  }
  92.     procedure AddDriveIcons( var XCounter , YCounter : Integer ); { Add drives }
  93.     procedure GetColorsForFileIcon( TheFile : String;
  94.                var BC , HC , SC , TC : TColor );
  95.     procedure GetIconsForEntireDirectory( TargetPath  : String );
  96.     function GetNextSelection( SourceDirectory : String;
  97.               var CurrentItem : Integer ) : String;
  98.   end;
  99.  
  100.   { This procedure gets an icon for a file using FindExecutable  }
  101.   { and ExtractIcon. (assumes file/dir is passed)                }
  102.   procedure GetIconForFile( TheName : String; var TheIcon : TIcon );
  103.   { This procedure spaces out the bitbtn components on a tpanel }
  104.   procedure SpacePanelButtons( WhichPanel : TPanel );
  105.  
  106. implementation
  107. {$R DRWSUTL2.RES}                 { Import custom resource file }
  108.  
  109. { This procedure spaces out the bitbtn components on a tpanel }
  110. procedure SpacePanelButtons( WhichPanel : TPanel );
  111. var TheCalculatedSpacing     ,            { Holds primary spacing }
  112.     TheFullCalculatedSpacing   : Integer; { Holds full spacing    }
  113.     Counter_1                  : Integer; { Loop counter          }
  114.     TotalIBs                   : Integer; { Gets total buttons    }
  115. begin
  116.   { Set up spacing values }
  117.   TotalIBs := WhichPanel.ControlCount;
  118.   TheCalculatedSpacing := (( WhichPanel.Width - 6 - ( TotalIbs * 49 ))
  119.    div ( TotalIbs + 1 ));
  120.   TheFullCalculatedSpacing := TheCalculatedSpacing + 49;
  121.   { Loop through all imported buttons and set their Left values }
  122.   for Counter_1 := 1 to WhichPanel.ControlCount do
  123.   begin
  124.     if Counter_1 = 1 then
  125.     begin
  126.       TBitBtn( WhichPanel.Controls[ Counter_1 - 1 ] ).Left := 3 +
  127.        TheCalculatedSpacing;
  128.     end
  129.     else
  130.     begin
  131.       TBitBtn( WhichPanel.Controls[ Counter_1 - 1 ] ).Left := 3 +
  132.        (( Counter_1 - 1 ) * TheFullCalculatedSpacing ) + TheCalculatedSpacing;
  133.     end;
  134.   end;
  135. end;
  136.  
  137. { This procedure gets an icon for a file using FindExecutable  }
  138. { and ExtractIcon. (assumes file/dir is passed)                }
  139. procedure GetIconForFile( TheName : String; var TheIcon : TIcon );
  140. var TheExt           : String; { File extension holder }
  141.     TheOtherPChar  ,           { Windows ASCIIZ string }
  142.     TheResultPChar ,           { Windows ASCIIZ string }
  143.     ThePChar         : PChar;  { Windows ASCIIZ string }
  144. begin
  145.   { Check for directory and if so get directory icon from RES file }
  146.   if (( FileGetAttr( TheName ) and faDirectory ) = faDirectory ) then
  147.   begin
  148.     { Set up the PChar to communicate with Windows }
  149.     GetMem( TheOtherPChar , 255 );
  150.     { Convert Pascal-style string to ASCIIZ Pchar }
  151.     StrPCopy( TheOtherPChar , 'DIRECTORY' );
  152.     { Use API call to return icon handle of Icon Resource in FILECTRL.RES }
  153.     TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  154.     { Release memory from PChar }
  155.     FreeMem( TheOtherPChar , 255 );
  156.     { Leave }
  157.     exit;
  158.   end;
  159.   { Assume archive file; get its extension }
  160.   TheExt := Uppercase( ExtractFileExt( TheName ));
  161.   { If not an executable/image file then use FindExecutable to get icon }
  162.   if (( TheExt <> '.EXE' ) and ( TheExt <> '.BAT' ) and
  163.       ( TheExt <> '.PIF' ) and ( TheExt <> '.COM' )) then
  164.   begin
  165.     { Grab three chunks of memory }
  166.     GetMem( TheOtherPChar , 255 );
  167.     GetMem( TheResultPChar , 255 );
  168.     GetMem( ThePChar , 255 );
  169.     { Set up the name and its directory in Windows string formats }
  170.     StrPCopy( ThePChar, TheName );
  171.     StrPCopy( TheOtherPChar , ExtractFilePath( TheName ));
  172.     { Use FindExecutable API call to get path and name of owning file }
  173.     if FindExecutable( ThePChar , TheOtherPChar , TheResultPChar ) > 31 then
  174.     begin
  175.       { If get a result of 32 or more then try to get first icon of owner }
  176.       { Using ExtractIcon API call; 0 indicates first icon.               }
  177.       TheIcon.Handle := ExtractIcon( hInstance , TheResultPchar , 0 );
  178.       { If a handle is 0 then no icon in owner, get default icon from RES file }
  179.       if TheIcon.Handle = 0 then
  180.       begin
  181.         GetMem( TheOtherPChar , 255 );
  182.         StrPCopy( TheOtherPChar , 'NOICON' );
  183.         TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  184.         FreeMem( TheOtherPChar , 255 );
  185.         exit;
  186.       end;
  187.     end
  188.     else
  189.     { if no assigned executable, then get default icon from RES file }
  190.     begin
  191.       GetMem( TheOtherPChar , 255 );
  192.       StrPCopy( TheOtherPChar , 'NOICON' );
  193.       TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  194.       FreeMem( TheOtherPChar , 255 );
  195.       exit;
  196.     end;
  197.     FreeMem( TheOtherPChar , 255 );
  198.     FreeMem( TheResultPChar , 255 );
  199.     FreeMem( ThePChar , 255 );
  200.   end
  201.   else
  202.   { Assume Windows Executable file, so get icon from it with ExtractIcon API }
  203.   begin
  204.     GetMem( ThePChar , 255 );
  205.     StrPCopy( ThePChar , TheName );
  206.     { If no icons in file then get default icon (note use FFFF for -1) }
  207.     if ExtractIcon( hInstance , ThePchar , 65535 ) = 0 then
  208.     begin
  209.       Freemem( ThePChar , 255 );
  210.       GetMem( TheOtherPChar , 255 );
  211.       StrPCopy( TheOtherPChar , 'NOICON' );
  212.       TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  213.       FreeMem( TheOtherPChar , 255 );
  214.       exit;
  215.     end
  216.     else
  217.     begin
  218.       { Try to get first icon for file }
  219.       TheIcon.Handle := ExtractIcon( hInstance , ThePChar , 0 );
  220.       FreeMem( ThePChar , 255 );
  221.       { If handle is 0 invalid icon format so use default from RES file }
  222.       if TheIcon.Handle = 0 then
  223.       begin
  224.         GetMem( TheOtherPChar , 255 );
  225.         StrPCopy( TheOtherPChar , 'NOICON' );
  226.         TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  227.         FreeMem( TheOtherPChar , 255 );
  228.         exit;
  229.       end;
  230.     end;
  231.   end;
  232. end;
  233.  
  234. { This procedure does a fully error-trapped change directory }
  235. procedure TFileWorkBench.ChangeTheDirectory( NewPath : String );
  236. var CurrentDirectory : String;
  237. begin
  238.   if NewPath = '..' then
  239.   begin { Back up one level }
  240.     {$I+}
  241.     try
  242.       { Find the current directory }
  243.       GetDir( 0 , CurrentDirectory );
  244.       { Use EFP to move up one level }
  245.       CurrentDirectory := ExtractFilePath( CurrentDirectory );
  246.       { Strip trailing \ if not root }
  247.       CurrentDirectory := StripNonRootTrailingBackSlash( CurrentDirectory );
  248.       { Try the change to the new drive }
  249.       ChDir( CurrentDirectory );
  250.     except
  251.       { if any exception occurs instantiate exception and show }
  252.       On E:EInOutError do
  253.       begin
  254.         { Call custom error display/lookup procedure }
  255.         HandleIOException( EOC_CHANGEDIR , CurrentDirectory ,
  256.          E.Message , E.ErrorCode );
  257.       end;
  258.     end;
  259.   end
  260.   else
  261.   begin { Change to explicit path }
  262.     {$I+}
  263.     try
  264.       { Get target directory path }
  265.       CurrentDirectory := NewPath;
  266.       { Strip trailing \ if not root }
  267.       CurrentDirectory := StripNonRootTrailingBackSlash( CurrentDirectory );
  268.       { Try the change to the new drive }
  269.       ChDir( CurrentDirectory );
  270.     except
  271.       { if any exception occurs instantiate exception and show }
  272.       On E:EInOutError do
  273.       begin
  274.         { Call custom error display/lookup procedure }
  275.         HandleIOException( EOC_CHANGEDIR , CurrentDirectory ,
  276.          E.Message , E.ErrorCode );
  277.       end;
  278.     end;
  279.   end;
  280. end;
  281.  
  282. { This procedure does a fully error-trapped change directory }
  283. procedure TFileWorkBench.ChangeTheDriveAndDirectory( NewDrive : Integer );
  284. var CurrentDirectory : String;
  285. begin
  286.   {$I+}
  287.   try
  288.     { Find the working directory on new drive }
  289.     GetDir( NewDrive , CurrentDirectory );
  290.     { Try the change to the new drive }
  291.     ChDir( CurrentDirectory );
  292.   except
  293.     { if any exception occurs instantiate exception and show }
  294.     On E:EInOutError do
  295.     begin
  296.       { Call custom error display/lookup procedure }
  297.       HandleIOException( EOC_CHANGEDIR , CurrentDirectory ,
  298.        E.Message , E.ErrorCode );
  299.     end;
  300.   end;
  301. end;
  302.  
  303. { This procedure copies a single file with error trapping }
  304. procedure TFileWorkBench.CopyTheFile( OldPath , NewPath : String );
  305. var AResult : Boolean; { Internal data flag }
  306. begin
  307.   { If Copyfile returns false an error occurred }
  308.   AResult := CopyFile( OldPath , NewPath +
  309.    ExtractFileName( OldPath ));
  310.   { Display meaningful error message }
  311.   if not AResult then HandleDOSError( GlobalErrorType ,
  312.    ExtractFileName( OldPath ) , GlobalError );
  313. end;
  314.  
  315. { This procedure moves a file by copying and delete it }
  316. procedure TFileWorkBench.MoveTheFile( OldPath , NewPath : String );
  317. var AResult : Boolean; { Internal data flag }
  318.     TheFile : File;    { Use to get errors  }
  319. begin
  320.   { If Copyfile returns false an error occurred }
  321.   AResult := CopyFile( OldPath , NewPath +
  322.     ExtractFileName( OldPath ));
  323.   { Display meaningful error message }
  324.   if not AResult then HandleDOSError( GlobalErrorType ,
  325.    ExtractFileName( OldPath ), GlobalError );
  326.   { After valid copying, delete source file }
  327.   {$I+}
  328.   if AResult then try
  329.     { Use this trick to get valid exception handling }
  330.     AssignFile( TheFile , OldPath );
  331.     { Use erase because Deletefile doesn't give exceptions! }
  332.     Erase( TheFile );
  333.   except
  334.     { if any exception occurs instantiate exception and show }
  335.     On E:EInOutError do
  336.     begin
  337.       { Call custom error display/lookup procedure }
  338.       HandleIOException( EOC_DELETEFILE , ExtractFileName( OldPath ) ,
  339.        E.Message , E.ErrorCode );
  340.     end;
  341.   end;
  342. end;
  343.  
  344. { This procedure safely deletes a single file }
  345. procedure TFileWorkBench.DeleteTheFile( ThePath : String );
  346. var TheFile : File; { Internal file handle }
  347. begin
  348.   {$I+}
  349.   try
  350.     { Use this trick to get valid exception handling }
  351.     AssignFile( TheFile , ThePath );
  352.     { Use erase because Deletefile doesn't give exceptions! }
  353.     Erase( TheFile );
  354.   except
  355.     { if any exception occurs instantiate exception and show }
  356.     On E:EInOutError do
  357.     begin
  358.       { Call custom error display/lookup procedure }
  359.       HandleIOException( EOC_DELETEFILE , ExtractFileName( ThePath ) ,
  360.        E.Message , E.ErrorCode );
  361.     end;
  362.   end;
  363. end;
  364.  
  365. { This procedure renames a file with full error trapping }
  366. procedure TFileWorkBench.RenameTheFile( OldPath , NewName : String );
  367. var TheFile : File; { Internal file handle }
  368. begin
  369.   {$I+}
  370.   try
  371.     { Use this trick to get valid exception handling }
  372.     AssignFile( TheFile , OldPath );
  373.     { Use this because RenameFile doesn't give exceptions! }
  374.     Rename( TheFile , NewName );
  375.   except
  376.     { if any exception occurs instantiate exception and show }
  377.     On E:EInOutError do
  378.     begin
  379.       { Call custom error display/lookup procedure }
  380.       HandleIOException( EOC_RENAMEFILE , ExtractFileName( OldPath ) ,
  381.        E.Message , E.ErrorCode );
  382.     end;
  383.   end;
  384. end;
  385.  
  386. { This procedure creates a new directory with full error trapping }
  387. procedure TFileWorkBench.CreateNewDirectory( NewPath : String );
  388. begin
  389.   {$I+}
  390.   try
  391.     Mkdir( NewPath );
  392.   except
  393.     { if any exception occurs instantiate exception and show }
  394.     On E:EInOutError do
  395.     begin
  396.       { Call custom error display/lookup procedure }
  397.       HandleIOException( EOC_MAKEDIR , ExtractFileName( NewPath ) ,
  398.        E.Message , E.ErrorCode );
  399.     end;
  400.   end;
  401. end;
  402.  
  403. { This procedure remove a directory with full error trapping }
  404. procedure TFileWorkBench.RemoveDirectory( ThePath : String );
  405. begin
  406.   {$I+}
  407.   try
  408.     Rmdir( ThePath );
  409.   except
  410.     { if any exception occurs instantiate exception and show }
  411.     On E:EInOutError do
  412.     begin
  413.       { Call custom error display/lookup procedure }
  414.       HandleIOException( EOC_DELETEDIR , ExtractFileName( ThePath ) ,
  415.        E.Message , E.ErrorCode );
  416.     end;
  417.   end;
  418. end;
  419.  
  420. { This is a generic copy routine taken from Delphi sample code }
  421. { It has been edited to return viable error codes!             }
  422. procedure TFileWorkBench.FMXUCopyFile(const FileName, DestName: String);
  423. var
  424.   CopyBuffer: Pointer; { buffer for copying }
  425.   BytesCopied: Longint;
  426.   TheAttr : Integer;
  427.   Source, Dest: Integer; { handles }
  428. const
  429.   ChunkSize: Longint = 8192; { copy in 8K chunks }
  430. begin
  431.   GetMem(CopyBuffer, ChunkSize); { allocate the buffer }
  432.   Source := FileOpen(FileName, fmShareDenyWrite); { open source file }
  433.   if Source < 0 then
  434.   begin  { error creating source file }
  435.     GlobalErrorType := EOC_SOURCECOPY;
  436.     GlobalError := -IOResult;
  437.     if GlobalError = 0 then GlobalError := -157;
  438.     FreeMem( CopyBuffer, ChunkSize );
  439.     exit;
  440.   end;
  441.   Dest := FileCreate(DestName); { create output file; overwrite existing }
  442.   if Dest < 0 then
  443.   begin  { error creating destination file }
  444.     FileClose( Source );
  445.     GlobalErrorType := EOC_DESTCOPY;
  446.     GlobalError := -IOResult;
  447.     if GlobalError = 0 then GlobalError := -159;
  448.     FreeMem( CopyBuffer , ChunkSize );
  449.     exit;
  450.   end;
  451.   {$I-}
  452.   repeat
  453.     BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize); { read chunk}
  454.     if BytesCopied > 0 then { if we read anything... }
  455.     FileWrite(Dest, CopyBuffer^, BytesCopied); { ...write chunk }
  456.   until BytesCopied < ChunkSize; { until we run out of chunks }
  457.   {$I+}
  458.   GlobalError := -IOResult;  { get any error code which happens during copying }
  459.   FileClose(Dest); { close the destination file }
  460.   FileClose(Source); { close the source file }
  461.   FreeMem(CopyBuffer, ChunkSize); { free the buffer }
  462. end;
  463.  
  464. { This function calls the sample Copy code and handles errors }
  465. function TFileWorkBench.CopyFile( TargetPath ,
  466.           DestinationPath : String ) : Boolean;
  467. begin
  468.   { Set global error value to no error }
  469.   GlobalError := 0;
  470.   { Call the sample procedure to do the copy }
  471.   FMXUCopyFile( TargetPath, DestinationPath );
  472.   { If no error return true else return false }
  473.   if GlobalError < 0 then CopyFile := false else
  474.    CopyFile := true;
  475. end;
  476.  
  477. { This procedure handles displaying a user-friendly Dialog box with a }
  478. { Message for Delphi IO exception errors.                             }
  479. procedure TFileWorkBench.HandleIOException( TheOpCode : Integer;
  480.            ThePath : String; TheMessage : String; TheCode : Integer );
  481. var ErrorMessageString : String;  { Holds internal data }
  482.     OperationString    : String;  { Holds internal data }
  483. begin
  484.   { clear to check for unrecognized code }
  485.   ErrorMessageString := '';
  486.   { Check against imported code }
  487.   case TheCode of
  488.     2    : ErrorMessageString := 'File not found';
  489.     3    : ErrorMessageString := 'Path not found';
  490.     4    : ErrorMessageString := 'Too many open files';
  491.     5    : ErrorMessageString := 'File access denied';
  492.     6    : ErrorMessageString := 'Invalid file handle';
  493.     12    : ErrorMessageString := 'Invalid file access code';
  494.     15    : ErrorMessageString := 'Invalid drive number';
  495.     16  : ErrorMessageString := 'Cannot remove current directory';
  496.     17    : ErrorMessageString := 'Cannot rename across drives';
  497.     100    : ErrorMessageString := 'Disk read error';
  498.     101    : ErrorMessageString := 'Disk write error';
  499.     102    : ErrorMessageString := 'File not assigned';
  500.     103    : ErrorMessageString := 'File not open';
  501.     104    : ErrorMessageString := 'File not open for input';
  502.     105    : ErrorMessageString := 'File not open for output';
  503.   end;
  504.   case TheOpCode of
  505.     EOC_CHANGEDIR : OperationString := 'Unable to Change Directory due to ';
  506.     EOC_SOURCECOPY : OperationString := 'Unable to Copy due to Source File ';
  507.     EOC_DESTCOPY : OperationString := 'Unable to Copy due to Destination File ';
  508.     EOC_DELETEFILE : OperationString := 'Unable to Delete File due to ';
  509.     EOC_DELETEDIR : OperationString := 'Unable to Delete Directory due to ';
  510.     EOC_RENAMEFILE : OperationString := 'Unable to Rename File due to ';
  511.     EOC_MAKEDIR : OperationString := 'Unable to Create New Directory due to ';
  512.     EOC_SETATTR : OperationString := 'Unable to Set File Attributes due to ';
  513.   end;
  514.   { If not recognized use message; not a DOS error; reset cursor for neatness }
  515.   if ErrorMessageString = '' then
  516.   begin
  517.     Screen.Cursor := crDefault;
  518.     MessageDlg( OperationString + ExtractFileName( ThePath ) + ' ' +
  519.      TheMessage , mtError , [mbOK],0);
  520.   end
  521.   else
  522.   begin
  523.     { Recognized DOS exception, reset cursor for neatness }
  524.     Screen.Cursor := crDefault;
  525.     MessageDlg( OperationString + ExtractFileName( ThePath ) + ' ' +
  526.      ErrorMessageString , mtError , [mbOK], 0 );
  527.   end;
  528. end;
  529.  
  530. { This procedure handles displaying a user-friendly Dialog box with a }
  531. { Message for DOS error codes.                                        }
  532. procedure TFileWorkBench.HandleDOSError( TheOpCode : Integer;
  533.            ThePath : String;  TheCode : Integer );
  534. var ErrorMessageString : String;  { internal message holder }
  535.     OperationString : String;     { internal message holder }
  536. begin
  537.   { clear the message holder to check for unrecognized code }
  538.   ErrorMessageString := '';
  539.   { Negate the code back to normal number and check to set string }
  540.   case -TheCode of
  541.     2    : ErrorMessageString := 'File not found';
  542.     3    : ErrorMessageString := 'Path not found';
  543.     4    : ErrorMessageString := 'Too many open files';
  544.     5    : ErrorMessageString := 'File access denied';
  545.     6    : ErrorMessageString := 'Invalid file handle';
  546.     12    : ErrorMessageString := 'Invalid file access code';
  547.     15    : ErrorMessageString := 'Invalid drive number';
  548.     16  : ErrorMessageString := 'Cannot remove current directory';
  549.     17    : ErrorMessageString := 'Cannot rename across drives';
  550.     100    : ErrorMessageString := 'Disk read error';
  551.     101    : ErrorMessageString := 'Disk write error';
  552.     102    : ErrorMessageString := 'File not assigned';
  553.     103    : ErrorMessageString := 'File not open';
  554.     104    : ErrorMessageString := 'File not open for input';
  555.     105    : ErrorMessageString := 'File not open for output';
  556.     157 : ErrormessageString := 'Could not open Source File';
  557.     159 : ErrormessageString := 'Could not open Target File';
  558.   end;
  559.   case TheOpCode of
  560.     EOC_CHANGEDIR : OperationString := 'Unable to Change Directory due to ';
  561.     EOC_SOURCECOPY : OperationString := 'Unable to Copy due to Source File ';
  562.     EOC_DESTCOPY : OperationString := 'Unable to Copy due to Destination File ';
  563.     EOC_DELETEFILE : OperationString := 'Unable to Delete File due to ';
  564.     EOC_DELETEDIR : OperationString := 'Unable to Delete Directory due to ';
  565.     EOC_RENAMEFILE : OperationString := 'Unable to Rename File due to ';
  566.     EOC_MAKEDIR : OperationString := 'Unable to Create New Directory due to ';
  567.     EOC_SETATTR : OperationString := 'Unable to Set File Attributes due to ';
  568.   end;
  569.   { If the string is empty an unrecognized code was sent in }
  570.   if ErrorMessageString = '' then
  571.   begin
  572.     { Sent up db based on source or target error; reset cursor for neatness }
  573.     Screen.Cursor := crDefault;
  574.     MessageDlg( OperationString + ExtractFileName( ThePath ) + ' Error Code: ' +
  575.      IntToStr( TheCode ) , mtError , [mbOK],0);
  576.   end
  577.   else  { Code is recognized, use message from case statement }
  578.   begin
  579.     { Format the output for source or target error }
  580.     Screen.Cursor := crDefault;
  581.     MessageDlg( OperationString + ExtractFilePath( ThePath ) + ' ' +
  582.      ErrorMessageString , mtError , [mbOK], 0 );
  583.   end;
  584. end;
  585.  
  586. { This procedure sets the imported booleans to the file's attributes }
  587. procedure TFileWorkBench.GetFileAttributes( TheFile : String; var IsDirectory ,
  588.            IsArchive , IsVolumeID , IsHidden , IsReadOnly ,
  589.             IsSysFile : Boolean );
  590. var TheResult : Integer; { Traps for error code on VolumeID }
  591. begin
  592.   { Clear the imported flags for default }
  593.   IsDirectory := false;
  594.   IsArchive := false;
  595.   IsVolumeID := false;
  596.   IsHidden := False;
  597.   IsReadOnly := false;
  598.   IsSysFile := false;
  599.   { Make the Dos call }
  600.   TheResult := FileGetAttr( TheFile );
  601.   if TheResult < 0 then
  602.   begin
  603.     { Volume ID returns -2 (?) }
  604.     IsVolumeID := true;
  605.     { It has no other properties }
  606.     exit;
  607.   end;
  608.   { Use AND test to set all other properties }
  609.   if (( TheResult and faDirectory ) = faDirectory ) then IsDirectory := true;
  610.   if (( TheResult and faArchive ) = faArchive ) then IsArchive := true;
  611.   if (( TheResult and faVolumeID ) = faVolumeID ) then IsVolumeID := true;
  612.   if (( TheResult and faReadOnly ) = faReadOnly ) then IsReadOnly := true;
  613.   if (( TheResult and faHidden ) = faHidden ) then IsHidden := true;
  614.   if (( TheResult and faSysFile ) = faSysFile ) then IsSysFile := true;
  615. end;
  616.  
  617. { This function makes sure a pathname has a trailing \ }
  618. function TFileWorkBench.ForceTrailingBackSlash(
  619.           const TheFileName : String ) : String;
  620. var TempString : String;  { Used to hold function result }
  621. begin
  622.   { If no trailing \ add one (root will already have one.) }
  623.   if TheFileName[ Length( TheFileName ) ] <> '\' then
  624.    TempString := TheFileName + '\' else TempString := TheFileName;
  625.   { Return modified or non-modified string }
  626.   ForceTrailingBackslash := TempString;
  627. end;
  628.  
  629. { This function makes sure a non-root dir has no trailing \ }
  630. function TFileWorkBench.StripNonRootTrailingBackSlash(
  631.           const TheFileName : String ) : String;
  632. var TempString : String ; { Used to hold function result }
  633. begin
  634.   { Default is no change }
  635.   TempString := TheFileName;
  636.   { If not root then }
  637.   if Length( TheFileName ) > 3 then
  638.   begin
  639.     { If has a trailing backslash remove it }
  640.     if TheFileName[ Length( TheFileName )] = '\' then
  641.     begin
  642.       TempString := Copy( TheFileName , 1 ,
  643.        Length( TheFileName ) - 1 );
  644.     end;
  645.   end;
  646.   { Export the final result }
  647.   StripNonRootTrailingBackSlash := TempString;
  648. end;
  649.  
  650. { Create method for FIP                                }
  651. constructor TFileIconPanel.Create( AOwner : TComponent );
  652. begin
  653.   { call inherited -- VITAL! }
  654.   inherited Create( AOwner );
  655.   { create icon and label components, making self owner/displayer }
  656.   FTheIcon := TIcon.Create;
  657.   FTheLabel := TLabel.Create( Self );
  658.   FThelabel.Parent := Self;
  659.   { Set own and labels mouse methods to stored methods }
  660.   OnClick := TheClick;
  661.   FTheLabel.OnClick := TheClick;
  662.   { Set alignment and autosize properties of the label }
  663.   FTheLabel.Autosize := false;
  664.   FTheLabel.Alignment := taCenter;
  665.   { Set selected to false }
  666.   Selected := false;
  667. end;
  668.  
  669. { Initialization method for FIP                                         }
  670. procedure TFileIconPanel.Initialize( PanelX              ,
  671.                                      PanelY              ,
  672.                                      PanelWidth          ,
  673.                                      PanelHeight         ,
  674.                                      PanelBevelWidth     ,
  675.                                      LabelFontSize         : Integer;
  676.                                      PanelColor          ,
  677.                                      PanelHighlightColor ,
  678.                                      PanelShadowColor    ,
  679.                                      LabelTextColor        : TColor;
  680.                                      TheFilename         ,
  681.                                      LabelFontName         : String;
  682.                                      LabelFontStyle        : TFontStyles;
  683.                                      ExtraData             : Integer );
  684.  
  685. var TheLabelHeight ,             { Holder for label pixel height }
  686.     TheLabelWidth    : Integer;  { Holder for label pixel width  }
  687.     TheOtherPChar    : PChar;    { Windows ASCIIZ string         }
  688. begin
  689.   { Set the basic properties based on imported parameters }
  690.   Left := PanelX;
  691.   Top := PanelY;
  692.   Width := PanelWidth;
  693.   Height := PanelHeight;
  694.   Color := PanelColor;
  695.   BevelWidth := PanelBevelWidth;
  696.   FHighlightColor := PanelHighlightColor;
  697.   FShadowColor := PanelShadowColor;
  698.   FTheName := TheFilename;
  699.   { If the ExtraData field is non-0 then a drive is being sent in }
  700.   if ExtraData <> 0 then
  701.   begin
  702.     { Use the data field value to determine which icon to get from RES file }
  703.     case ExtraData of
  704.       1 : begin
  705.             GetMem( TheOtherPChar , 255 );
  706.             StrPCopy( TheOtherPChar , 'FLOPPY35' );
  707.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  708.             FreeMem( TheOtherPChar , 255 );
  709.           end;
  710.       2 : begin
  711.             GetMem( TheOtherPChar , 255 );
  712.             StrPCopy( TheOtherPChar , 'FIXEDHD' );
  713.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  714.             FreeMem( TheOtherPChar , 255 );
  715.           end;
  716.       3 : begin
  717.             GetMem( TheOtherPChar , 255 );
  718.             StrPCopy( TheOtherPChar , 'NETWORKHD' );
  719.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  720.             FreeMem( TheOtherPChar , 255 );
  721.           end;
  722.       4 : begin
  723.             GetMem( TheOtherPChar , 255 );
  724.             StrPCopy( TheOtherPChar , 'CDROM' );
  725.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  726.             FreeMem( TheOtherPChar , 255 );
  727.           end;
  728.       5 : begin
  729.             GetMem( TheOtherPChar , 255 );
  730.             StrPCopy( TheOtherPChar , 'RAM' );
  731.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  732.             FreeMem( TheOtherPChar , 255 );
  733.           end;
  734.     end;
  735.     { The FileNme property is already set up for the caption; use directly }
  736.     FTheLabel.Caption := TheFilename;
  737.     { Set up the hint for later use (make sure to set ShowHint) }
  738.     Hint := 'Change to ' + TheFileName;
  739.     ShowHint := true;
  740.     { Set up all imported label properties and center it for drawing }
  741.     with FTheLabel do
  742.     begin
  743.       Font.Name := LabelFontName;
  744.       Font.Size := LabelFontSize;
  745.       Font.Style := LabelFontStyle;
  746.       Font.Color := LabelTextColor;
  747.       Canvas.Brush.Color := PanelColor;
  748.       Canvas.Font := Font;
  749.       TheLabelHeight := Canvas.Textheight( Caption ) + 4;
  750.       TheLabelWidth := Canvas.Textwidth( Caption ) + 4;
  751.       Left := (( Self.Width - TheLabelWidth ) div 2 ) + 1;
  752.       Top := ((( Round( Self.Height * 0.25 ) - 6 ) - TheLabelHeight) div 2) + 1;
  753.       Top := Top + Round( Self.Height * 0.75 );
  754.       Height := TheLabelHeight;
  755.       Width := TheLabelWidth;
  756.     end;
  757.   end
  758.   else
  759.   begin
  760.     { A file or directory has been sent in; use GetIconForFile to obtain an }
  761.     { icon either from the file, its owner, or a RES file default.          }
  762.     GetIconForFile( FTheName , FTheIcon );
  763.     { Check for the Backup caption and set it specially }
  764.     if ExtractfileName( FThename ) = '..' then
  765.     begin
  766.       FTheLabel.Caption := '..';
  767.       Hint := 'Up One Level';
  768.     end
  769.     else
  770.     begin
  771.       { Otherwise just get the filename for the label caption }
  772.       { And the full path for the hint (used later.)          }
  773.       FTheLabel.caption := ExtractFileName( UpperCase( FTheName ));
  774.       Hint := FTheName;
  775.     end;
  776.     { Activate showhint so hints are seen }
  777.     ShowHint := true;
  778.     { Set label properties with imported values and center for display }
  779.     with FTheLabel do
  780.     begin
  781.       Font.Name := LabelFontName;
  782.       Font.Size := LabelFontSize;
  783.       Font.Style := LabelFontStyle;
  784.       Font.Color := LabelTextColor;
  785.       Canvas.Brush.Color := PanelColor;
  786.       Canvas.Font := Font;
  787.       TheLabelHeight := Canvas.Textheight( Caption ) + 4;
  788.       TheLabelWidth := Canvas.Textwidth( Caption ) + 4;
  789.       Left := (( Self.Width - TheLabelWidth ) div 2 ) + 1;
  790.       Top := ((( Round( Self.Height * 0.25 ) - 6 ) - TheLabelHeight) div 2) + 1;
  791.       Top := Top + Round( Self.Height * 0.75 );
  792.       Height := TheLabelHeight;
  793.       Width := TheLabelWidth;
  794.     end;
  795.   end;
  796. end;
  797.  
  798. { Destroy method for FIP }
  799. destructor TFileIconPanel.Destroy;
  800. begin
  801.   { free component resources }
  802.   FTheIcon.Free;
  803.   FTheLabel.Free;
  804.   { call inherited -- VITAL! }
  805.   inherited Destroy;
  806. end;
  807.  
  808. { TheClick method for FIP; used for event responses }
  809. procedure TFileIconPanel.TheClick( Sender : TObject );
  810. begin
  811.   { Currently ignore drive clicks }
  812.   if Pos( 'DRIVE' , FTheName ) > 0 then exit;
  813.   { Flip status of bevels }
  814.   if BevelOuter = bvRaised then BevelOuter := bvLowered else
  815.    BevelOuter := bvRaised;
  816.   { Flip selected variable }
  817.   Selected := not Selected;
  818.   { Set redisplay }
  819.   Invalidate;
  820. end;
  821.  
  822. { Paint method for FIP; overrides normal paint }
  823. procedure TFileIconPanel.Paint;
  824. var
  825.   TheOtherRect   : TRect;   { Holds clientrect   }
  826.   TopColor     ,            { Holds bright color }
  827.   BottomColor    : TColor;  { Holds dark color   }
  828.  
  829. { These methods are from Borland Intl., copyright 1995 }
  830. procedure Frame3D(    Canvas       : TCanvas;
  831.                   var TheRect      : TRect;
  832.                       TopColor   ,
  833.                       BottomColor  : TColor;
  834.                       Width        : Integer );
  835.  
  836. procedure DoRect;
  837. var
  838.   TopRight, BottomLeft: TPoint;
  839. begin
  840.   with Canvas, TheRect do
  841.   begin
  842.     TopRight.X := Right;
  843.     TopRight.Y := Top;
  844.     BottomLeft.X := Left;
  845.     BottomLeft.Y := Bottom;
  846.     Pen.Color := TopColor;
  847.     PolyLine([BottomLeft, TopLeft, TopRight]);
  848.     Pen.Color := BottomColor;
  849.     Dec(BottomLeft.X);
  850.     PolyLine([TopRight, BottomRight, BottomLeft]);
  851.   end;
  852. end;
  853.  
  854. begin
  855.   Canvas.Pen.Width := 1;
  856.   Dec(TheRect.Bottom); Dec(TheRect.Right);
  857.   while Width > 0 do
  858.   begin
  859.     Dec(Width);
  860.     DoRect;
  861.     InflateRect(TheRect, -1, -1);
  862.   end;
  863.   Inc(TheRect.Bottom); Inc(TheRect.Right);
  864. end;
  865.  
  866. procedure AdjustColors(Bevel: TPanelBevel);
  867. begin
  868.   TopColor := FHighlightColor;
  869.   if Bevel = bvLowered then TopColor := FShadowColor;
  870.   BottomColor := FShadowColor;
  871.   if Bevel = bvLowered then BottomColor := FHighlightColor;
  872. end;
  873.  
  874. { Custom code begins here }
  875. begin
  876.   { Get the rectangle of the control with API/method call }
  877.   TheOtherRect := GetClientRect;
  878.   { draw basic rectangle with basic color }
  879.   with Canvas do
  880.   begin
  881.     Brush.Color := Color;
  882.     FillRect(TheOtherRect);
  883.   end;
  884.   { Set up for top "icon" frame  and draw it with frame3d }
  885.   TheOtherRect.Right := Width;
  886.   TheOtherRect.Bottom := Round( Height * 0.75 ) - 6 ;
  887.   if BevelOuter <> bvNone then
  888.   begin
  889.     AdjustColors(BevelOuter);
  890.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  891.   end;
  892.   Frame3D(Canvas, TheOtherRect, Color, Color, BorderWidth);
  893.   if BevelInner <> bvNone then
  894.   begin
  895.     AdjustColors(BevelInner);
  896.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  897.   end;
  898.   { Do the same for the lower "label" frame }
  899.   TheOtherRect.Top := Round( Height * 0.75 ) - 5;
  900.   TheOtherRect.Left := 0;
  901.   TheOtherRect.Bottom := Height;
  902.   TheOtherRect.Right := Width;
  903.   if BevelOuter <> bvNone then
  904.   begin
  905.     AdjustColors(BevelOuter);
  906.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  907.   end;
  908.   Frame3D(Canvas, TheOtherRect, Color, Color, BorderWidth);
  909.   if BevelInner <> bvNone then
  910.   begin
  911.     AdjustColors(BevelInner);
  912.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  913.   end;
  914.   { Then draw the icon using canvas draw method }
  915.   Canvas.Draw( (( Width - 32 ) div 2 ) + 1 ,
  916.   ((( Round( Height * 0.75 ) - 6 ) - 32 ) div 2 ) + 1 , FTheIcon );
  917. end;
  918.  
  919. { This procedure clears a scrollbox of all FileIconPanels }
  920. procedure TFileIconPanelScrollbox.ClearTheFIPs;
  921. var Counter_1 : Integer;
  922.     TheComponent : TComponent;
  923. begin
  924.   { Note that must use while loop since component count continually }
  925.   { decreases as removes are made!                                  }
  926.   while ComponentCount > 0 do
  927.   begin
  928.     { Save the component as a generic TComponent }
  929.     TheComponent := Components[ 0 ];
  930.     { Call removecomponent to pull it out of the owner list for sb }
  931.     { This avoids GPF when freeing the sb.                         }
  932.     RemoveComponent( Components[ 0 ]);
  933.     { Typecast the pointer and free it to release memory and res. }
  934.     TFileIconPanel( TheComponent ).Free;
  935.   end;
  936. end;
  937.  
  938. { This procedure scans for drives and obtains their type and creates file }
  939. { icon panels to represent them.                                          }
  940. procedure TFileIconPanelScrollBox.AddDriveIcons( var XCounter ,
  941.            YCounter : Integer );
  942. type
  943.   { This if from filectrl unit; reproduce here for completeness }
  944.   TDriveType = (dtUnknown, dtNoDrive, dtFloppy, dtFixed, dtNetwork, dtCDROM,
  945.                 dtRAM);
  946. var
  947.   DriveNum        : Integer;         { Used to get next drive via DOS fn   }
  948.   IconType        : Integer;         { Used to hold icon type (defacto dt) }
  949.   DriveChar       : Char;            { Used to hold drive letter           }
  950.   DriveType       : TDriveType;      { Used for set-valued drive type      }
  951.   Finished        : Boolean;         { Loop flag                           }
  952.   TheFIP          : TFileIconPanel;  { Generic FileIconPanel variable      }
  953.   ButtonColor   ,                    { Main panel color                    }
  954.   ButtonHLColor ,                    { Bright panel color                  }
  955.   ButtonSColor  ,                    { Dark panel color                    }
  956.   Textcolor       : TColor;          { Label text color                    }
  957.  
  958. { This code is from the FileCtrl Unit; copyright Borland Intl 1995      }
  959. { Check whether drive is a CD-ROM.  Returns True if MSCDEX is installed }
  960. {  and the drive is using a CD driver                                   }
  961.  
  962. function IsCDROM(DriveNum: Integer): Boolean; assembler;
  963. asm
  964.   MOV   AX,1500h { look for MSCDEX }
  965.   XOR   BX,BX
  966.   INT   2fh
  967.   OR    BX,BX
  968.   JZ    @Finish
  969.   MOV   AX,150Bh { check for using CD driver }
  970.   MOV   CX,DriveNum
  971.   INT   2fh
  972.   OR    AX,AX
  973.   @Finish:
  974. end;
  975.  
  976. { This code is from the FileCtrl Unit; copyright Borland Intl 1995      }
  977. { Check whether drive is a RAM drive.                                   }
  978. function IsRAMDrive(DriveNum: Integer): Boolean; assembler;
  979. var
  980.   TempResult: Boolean;
  981. asm
  982.   MOV   TempResult,False
  983.   PUSH  DS
  984.   MOV   BX,SS
  985.   MOV   DS,BX
  986.   SUB   SP,0200h
  987.   MOV   BX,SP
  988.   MOV   AX,DriveNum
  989.   MOV   CX,1
  990.   XOR   DX,DX
  991.   INT   25h  { read boot sector }
  992.   ADD   SP,2
  993.   JC    @ItsNot
  994.   MOV   BX,SP
  995.   CMP   BYTE PTR SS:[BX+15h],0F8h  { reverify fixed disk }
  996.   JNE   @ItsNot
  997.   CMP   BYTE PTR SS:[BX+10h],1  { check for single FAT }
  998.   JNE   @ItsNot
  999.   MOV   TempResult,True
  1000.   @ItsNot:
  1001.   ADD   SP,0200h
  1002.   POP   DS
  1003.   MOV   AL, TempResult
  1004. end;
  1005.  
  1006. { This code is from the FileCtrl Unit; copyright Borland Intl 1995      }
  1007. { Finds the type of a drive letter.                                     }
  1008. function FindDriveType(DriveNum: Integer): TDriveType;
  1009. begin
  1010.   Result := TDriveType(GetDriveType(DriveNum));
  1011.   if (Result = dtFixed) or (Result = dtNetwork) then
  1012.   begin
  1013.     if IsCDROM(DriveNum) then Result := dtCDROM
  1014.     else if (Result = dtFixed) then
  1015.     begin
  1016.         { do not check for RAMDrive under Windows NT }
  1017.       if ((GetWinFlags and $4000) = 0) and IsRAMDrive(DriveNum) then
  1018.         Result := dtRAM;
  1019.     end;
  1020.   end;
  1021. end;
  1022.  
  1023. begin
  1024.   { Set the button colors to an aquamarine color scheme for drives }
  1025.   ButtonColor := clTeal;
  1026.   ButtonHLColor := clAqua;
  1027.   ButtonSColor := clNavy;
  1028.   TextColor := clblack;
  1029.   { Set initial variables before looping for all drives }
  1030.   finished := false;
  1031.   DriveNum := 0;
  1032.   while not finished do
  1033.   begin
  1034.     { Start with no drive found }
  1035.     IconType := 0;
  1036.     { Call the Borland method to get the drive info }
  1037.     DriveType := FindDriveType(DriveNum);
  1038.     { Set its letter and make it uppercase }
  1039.     DriveChar := Chr(DriveNum + ord('a'));
  1040.     DriveChar := Upcase(DriveChar);
  1041.     { Assign an icon based on the drive type; if no drive exists type is nil }
  1042.     case DriveType of
  1043.       dtFloppy  : IconType := 1;
  1044.       dtFixed   : IconType := 2;
  1045.       dtNetwork : IconType := 3;
  1046.       dtCDROM   : IconType := 4;
  1047.       dtRAM     : IconType := 5;
  1048.     end;
  1049.     { Set to check next drive letter }
  1050.     DriveNum := DriveNum + 1;
  1051.     { But if no match then out of drives so set exit flag }
  1052.     if IconType = 0 then finished := true;
  1053.     { If drive was valid then set up the new FileIconPanel on the imported }
  1054.     { Scrollbox                                                            }
  1055.     if not finished then
  1056.     begin
  1057.       { Create the FileIconPanel and set its parent for memory mgmt and display}
  1058.       TheFIP := TFileIconPanel.Create( Self );
  1059.       TheFIP.Parent := Self;
  1060.       { Call its initialize method with imported position values and the   }
  1061.       { preset color scheme, a drive caption, and a minimum font. Note the }
  1062.       { setting of the ExtraData field to non-zero; this signals a drive   }
  1063.       { rather than a file being sent in.                                  }
  1064.       TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1065.        (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize , 3 ,
  1066.         7 , ButtonColor, ButtonHLColor,
  1067.        ButtonSColor , TextColor , 'DRIVE ' + DriveChar + ':' , 'MS Serif' , [] ,
  1068.        IconType );
  1069.       { Increment the column counter; if it exceeds max move to new row      }
  1070.       { Note that these are 'var' parameters and will export final position. }
  1071.       XCounter := XCounter + 1;
  1072.       if XCounter > MaxIconsInARow then
  1073.       begin
  1074.         XCounter := 1;
  1075.         YCounter := YCounter + 1;
  1076.       end;
  1077.     end;
  1078.   end;
  1079. end;
  1080.  
  1081. { This procedure assigns colors to FIP's based on file attributes }
  1082. procedure TFileIconPanelScrollBox.GetColorsForFileIcon( TheFile : String;
  1083.            var BC , HC , SC , TC : TColor );
  1084. var AmADir      ,             { Booleans hold file attribs }
  1085.     AmAnArchive ,
  1086.     AmAVolumeId ,
  1087.     AmHidden    ,
  1088.     AmReadOnly  ,
  1089.     AmSystem      : Boolean;
  1090. begin
  1091.   { Make the call to internal fileworkbench to set attributes }
  1092.   TheFWB.GetFileAttributes( TheFile , AmADir , AmAnArchive , AmAVolumeId ,
  1093.    AmHidden , AmReadOnly , AmSystem );
  1094.   { Volume ID has no subtypes }
  1095.   if AmAVolumeID then
  1096.   begin
  1097.     BC := clOlive;
  1098.     HC := clYellow;
  1099.     SC := clBlack;
  1100.     TC := clWhite;
  1101.     exit;
  1102.   end;
  1103.   { Check all directory combinations }
  1104.   if AmADir then
  1105.   begin
  1106.     BC := clNavy;
  1107.     HC := clBlue;
  1108.     SC := clBlack;
  1109.     TC := clWhite;
  1110.     if AmHidden then
  1111.     begin
  1112.       if AmReadOnly then
  1113.       begin
  1114.         if AmSystem then
  1115.         begin { One HECK of a file! }
  1116.           BC := clBlack;
  1117.           HC := clSilver;
  1118.           SC := clGray;
  1119.           TC := clWhite;
  1120.         end
  1121.         else
  1122.         begin { Dir,RO,Hid }
  1123.           BC := clMaroon;
  1124.           HC := clFuchsia;
  1125.           SC := clGreen;
  1126.           TC := clWhite;
  1127.         end;
  1128.       end
  1129.       else
  1130.       begin { Dir,Hid }
  1131.         BC := clPurple;
  1132.         HC := clFuchsia;
  1133.         SC := clBlack;
  1134.         TC := clWhite;
  1135.       end;
  1136.     end
  1137.     else
  1138.     begin
  1139.       if AmReadOnly then
  1140.       begin
  1141.         if AmSystem then
  1142.         begin { Dir,RO,Sys }
  1143.           BC := clMaroon;
  1144.           HC := clLime;
  1145.           SC := clGreen;
  1146.           TC := clWhite;
  1147.         end
  1148.         else
  1149.         begin { Dir,RO }
  1150.           BC := clGreen;
  1151.           HC := clLime;
  1152.           SC := clBlack;
  1153.           TC := clWhite;
  1154.         end;
  1155.       end
  1156.       else
  1157.       begin
  1158.         if AmSystem then
  1159.         begin { Dir,Sys }
  1160.           BC := clMaroon;
  1161.           HC := clRed;
  1162.           SC := clBlack;
  1163.           TC := clWhite;
  1164.         end;
  1165.       end;
  1166.     end;
  1167.   end
  1168.   else { Archive Only; check all combinations }
  1169.   begin
  1170.     BC := clSilver;
  1171.     HC := clWhite;
  1172.     SC := clGray;
  1173.     TC := clBlack;
  1174.     if AmHidden then
  1175.     begin
  1176.       if AmReadOnly then
  1177.       begin
  1178.         if AmSystem then
  1179.         begin { Hid,RO,Sys }
  1180.           BC := clRed;
  1181.           HC := clLime;
  1182.           SC := clPurple;
  1183.           TC := clBlack;
  1184.         end
  1185.         else
  1186.         begin { RO,Hid }
  1187.           BC := clLime;
  1188.           HC := clFuchsia;
  1189.           SC := clMaroon;
  1190.           TC := clBlack;
  1191.         end;
  1192.       end
  1193.       else
  1194.       begin { Hid }
  1195.         BC := clFuchsia;
  1196.         HC := clWhite;
  1197.         SC := clPurple;
  1198.         TC := clBlack;
  1199.       end;
  1200.     end
  1201.     else
  1202.     begin
  1203.       if AmReadOnly then
  1204.       begin
  1205.         if AmSystem then
  1206.         begin { RO,Sys }
  1207.           BC := clRed;
  1208.           HC := clLime;
  1209.           SC := clMaroon;
  1210.           TC := clBlack;
  1211.         end
  1212.         else
  1213.         begin { RO }
  1214.           BC := clLime;
  1215.           HC := clWhite;
  1216.           SC := clGreen;
  1217.           TC := clBlack;
  1218.         end;
  1219.       end
  1220.       else
  1221.       begin
  1222.         if AmSystem then
  1223.         begin { System }
  1224.           BC := clRed;
  1225.           HC := clWhite;
  1226.           SC := clMaroon;
  1227.           TC := clBlack;
  1228.         end;
  1229.       end;
  1230.     end;
  1231.   end;
  1232. end;
  1233.  
  1234. { This procedure gets all icons for an given directory, including drives and }
  1235. { standard subdirectories. It does not get special combinations or h/ro/sys  }
  1236. procedure TFileIconPanelScrollbox.GetIconsForEntireDirectory(
  1237.             TargetPath  : String );
  1238. var Finished        : Boolean;         { Loop flag              }
  1239.     TheSR           : TSearchRec;      { Searchrecord for FF/FN }
  1240.     TheResult       : Integer;         { return variable        }
  1241.     TempPath        : String;          { path for FF/FN         }
  1242.     TheFIP          : TFileIconPanel;  { generic FIP holder     }
  1243.     RowCounter    ,                    { position in row of FIP }
  1244.     ColumnCounter   : Integer;         { position in col of FIP }
  1245.     ButtonColor   ,                    { main panel color       }
  1246.     ButtonHLColor ,                    { bright panel color     }
  1247.     ButtonSColor  ,                    { dark panel color       }
  1248.     Textcolor       : TColor;          { label text color       }
  1249.     IsADir ,                           { Variable for file attr }
  1250.     IsAnArchive ,
  1251.     IsAVolumeID,
  1252.     IsAReadOnlyFile,
  1253.     IsAHiddenFile ,
  1254.     IsASystemFile     : Boolean;
  1255.     MaxTextLength     : Integer;       { Used to safely set size}
  1256. begin
  1257.   { hide during refresh }
  1258.   Visible := false;
  1259.   { Delete the current set, if any }
  1260.   ClearTheFIPs;
  1261.   { Get the icon sizes }
  1262.   TheFIP := TFileIconPanel.Create( Self );
  1263.   TheFIP.Parent := Self;
  1264.   TheFIP.FTheLabel.Canvas.Font.Name := 'MS Serif';
  1265.   TheFIP.FTheLabel.Canvas.Font.Size := 7;
  1266.   MaxTextLength := TheFIP.FTheLabel.Canvas.TextWidth( 'COMMAND.COM' );
  1267.   TheFIP.Free;
  1268.   TheIconSize := MaxTextLength + 13;
  1269.   TheIconSpacing := TheIconSize + 5;
  1270.   { Set up maximum icons per row based on screen size }
  1271.   MaxIconsInARow := ( Screen.Width div TheIconSpacing );
  1272.   { Set up the position counters }
  1273.   RowCounter := 1;
  1274.   ColumnCounter := 1;
  1275.   { Get the drives for the current machine }
  1276.   AddDriveIcons( ColumnCounter , RowCounter  );
  1277.   { Set up the initial variables }
  1278.   Finished := false;
  1279.   TempPath := TargetPath + '*.*';
  1280.   { Make the call to FindFirst set to get any file; will return '.' }
  1281.   { so discard it.                                                  }
  1282.   TheResult := FindFirst( TempPath , faAnyFile , TheSR );
  1283.   { loop through all files in the directory and look for directories }
  1284.   while not Finished do
  1285.   begin
  1286.     { Make call to FindNext, using only SearchRecord from FindFirst }
  1287.     TheResult := FindNext( TheSR );
  1288.     { A -1 result means no more files so exit }
  1289.     if TheResult < 0 then finished := true else
  1290.     begin
  1291.       { Otherwise check for a directory attribute }
  1292.       if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory ) =
  1293.        faDirectory ) then
  1294.       begin
  1295.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1296.          ButtonHLColor , ButtonSColor , TextColor );
  1297.         { If found create a new FileIconPanel on the imported scrollbox }
  1298.         { Note sending 0 ExtraData parameter to indicate file not drive }
  1299.         TheFIP := TFileIconPanel.Create( Self );
  1300.         TheFIP.Parent := Self;
  1301.         TheFIP.Initialize((( ColumnCounter - 1 ) * TheIconSpacing ),
  1302.          (( RowCounter - 1  ) * TheIconSpacing ) , TheIconSize, TheIconSize ,
  1303.           3 , 7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1304.            TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  1305.         { Increment column counter and move to new row if past limit }
  1306.         ColumnCounter := ColumnCounter + 1;
  1307.         if ColumnCounter > MaxIconsInARow then
  1308.         begin
  1309.           ColumnCounter := 1;
  1310.           RowCounter := RowCounter + 1;
  1311.         end;
  1312.       end;
  1313.     end;
  1314.   end;
  1315.   { Call FindClose for Windows NT/Windows 95 compatibility }
  1316.   FindClose( TheSR );
  1317.   { Set up new initialization variables }
  1318.   Finished := false;
  1319.   TempPath := TargetPath + '*.*';
  1320.   { Make needed call to FindFirst and discard '.' }
  1321.   TheResult := FindFirst( TempPath , faAnyFile , TheSR );
  1322.   while not Finished do
  1323.   begin
  1324.     { Loop through file again, this time getting only archive files }
  1325.     TheResult := FindNext( TheSR );
  1326.     { Result of -1 indicates no more files }
  1327.     if TheResult < 0 then Finished := true else
  1328.     begin
  1329.       { If faArchive file then add new FileIconPanel }
  1330.       TheFWB.GetFileAttributes(( Targetpath + TheSR.Name ) , IsADir ,
  1331.        IsAnArchive , IsAVolumeId , IsAHiddenFile , IsAReadOnlyFile ,
  1332.         IsASystemFile );
  1333.       if (( IsAnArchive ) and ( not IsADir )) then
  1334.       begin
  1335.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1336.          ButtonHLColor , ButtonSColor , TextColor );
  1337.         { Initialize new FileIconPanel and call initialize, sending 0 ED }
  1338.         TheFIP := TFileIconPanel.Create( Self );
  1339.         TheFIP.Parent := Self;
  1340.         TheFIP.Initialize((( ColumnCounter - 1 ) * TheIconSpacing ),
  1341.          (( RowCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize ,
  1342.           3 , 7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1343.            TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  1344.         { Increment column counter and if needed row counter }
  1345.         ColumnCounter := ColumnCounter + 1;
  1346.         if ColumnCounter > MaxIconsInARow then
  1347.         begin
  1348.           ColumnCounter := 1;
  1349.           RowCounter := RowCounter + 1;
  1350.         end;
  1351.       end;
  1352.     end;
  1353.   end;
  1354.   { Call findclose for w95 and exit }
  1355.   FindClose( TheSR );
  1356.   { Reset to visible }
  1357.   Visible := true;
  1358. end;
  1359.  
  1360. { Update method for FIPscrollbox }
  1361. procedure TFileIconPanelScrollBox.Update;
  1362. begin
  1363.   IconsNeedRefreshing := true;
  1364.   { Force a repaint }
  1365.   InvalidateRect( TheStoredHandle , nil , true );
  1366. end;
  1367.  
  1368. { Create method for FIPScrollbox }
  1369. constructor TFileIconPanelScrollBox.Create( AOwner : TComponent );
  1370. begin
  1371.   inherited Create( AOwner );
  1372.   TheFWB := TFileWorkBench.Create( Self );
  1373. end;
  1374.  
  1375. { This function returns the next selected file's name }
  1376. function TFileIconPanelScrollBox.GetNextSelection( SourceDirectory : String;
  1377.                            var CurrentItem : Integer ) : String;
  1378. var TheResult    : String;      { Holds result of function }
  1379.     TheComponent : TComponent;  { Used for typecast        }
  1380.     finished     : boolean;     { Loop control variable    }
  1381.     TheComponentCount : Integer;
  1382. begin
  1383.   TheComponentCount := ComponentCount;
  1384.   { If past end of components exit with no result }
  1385.   if CurrentItem > TheComponentCount then TheResult := '' else
  1386.   begin
  1387.     { Set loop counter and run till find match or run out }
  1388.     finished := false;
  1389.     while not finished do
  1390.     begin
  1391.       { Pull component out of the list and check it }
  1392.       TheComponent := Components[ CurrentItem - 1 ];
  1393.       { Increment counter for later }
  1394.       CurrentItem := CurrentItem + 1;
  1395.       { Do the typecast with AS }
  1396.       with TheComponent as TFileIconPanel do
  1397.       begin
  1398.         { If its selected make sure OK }
  1399.         if Selected then
  1400.         begin
  1401.           { Don't accept backup for this level of operation }
  1402.           if FTheLabel.Caption <> '..' then
  1403.           begin
  1404.             { Otherwise return the name and abort the loop }
  1405.             TheResult := FTheName;
  1406.             finished := true;
  1407.           end;
  1408.         end
  1409.         else
  1410.         begin
  1411.           { Check to see if out of components }
  1412.           if CurrentItem > TheComponentCount then
  1413.           begin
  1414.             { If so signal error and abort }
  1415.             TheResult := '';
  1416.             finished := true;
  1417.           end;
  1418.         end;
  1419.       end;
  1420.     end;
  1421.   end;
  1422.   GetNextSelection := TheResult;
  1423. end;
  1424.  
  1425. end.
  1426.